package com.yanshibo.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.yanshibo.bean.Covo;
import com.yanshibo.bean.Drom;
import com.yanshibo.bean.Floor;
import com.yanshibo.bean.Record;
import com.yanshibo.bean.Student;
import com.yanshibo.bean.Tiaos;
import com.yanshibo.enums.Role;
import com.yanshibo.service.StudentService;

@Controller
public class StudentController {
	//注入值
	@Autowired
	private StudentService service;
	
	//跳转登录
	@RequestMapping("login")
	public String login(){
		return "login";
	}
	//登录
	@RequestMapping("toLogin")
	public String toLogin(Model model,Student student,HttpServletRequest request){
		//调用方法
		Student s = service.selUser(student);
		request.getSession().setAttribute("student", s);
		Role role = s.getRole();
		int r = role.ordinal();
		if(r==0){
			return "stuList";
		}else{
			return "admList";
		}
		
	}
	
	//列表
	@RequestMapping("stuList")
	public String stuList(HttpServletRequest request,Model model){
		Student student = (Student) request.getSession().getAttribute("student");
		Integer sid = student.getId();
		
		List<Record> list = service.selRec(sid);
		model.addAttribute("list", list);
		return "stuList";
	}
	//管理员列表
	@RequestMapping("admList")
	public String admList(HttpServletRequest request,Model model,Covo vo,@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="3")Integer pageSize){
		PageInfo<Drom> info = service.selDrom(pageNum,pageSize,vo);
		model.addAttribute("info", info);
		model.addAttribute("vo", vo);
		return "admList";
	}
	
	//查看
	@RequestMapping("ck")
	public String ck(Model model,Integer id){
		//1
		Drom drom = service.selById(id);
		model.addAttribute("drom", drom);
		//2
		List<Record> list = service.selReById(id);
		model.addAttribute("list", list);
		return "look";
	}
	
	//跳转调寝
	@RequestMapping("tq")
	public String tq(Model model){
		List<Student> list = service.selStu();
		model.addAttribute("list", list);
		return "tiao";
	}
	//查询楼
	@RequestMapping("selFloor")
	@ResponseBody
	public List<Floor> selFloor(){
		return service.selFloor();
	}
	
	@RequestMapping("selDrom")
	@ResponseBody
	public List<Drom> selDrom(Integer fid){
		return service.selDrom(fid);
	}
	
	//调室
	@RequestMapping("tiaos")
	public String tiaos(Tiaos t){
		service.tiaos(t);
		return "redirect:/admList";
	}
}
